首页 > 其他分享 >[Signal] 1 - Basic version of signal

[Signal] 1 - Basic version of signal

时间:2023-10-05 16:34:51浏览次数:37  
标签:function const Signal value stack version subscribers createSignal signal

Implement two functions of Signals

  • createSignal: return a tuple with read function and wrtie function
  • createEffect: accept a function which will be executed when signal value changed

 

Code to test:

import { createSignal } from "./reactivy";

const [count, setCount] = createSignal(0);

console.log(count()); // 0
setCount(5);
console.log(count()); // 5

 

Version 1 - CreateSignal

export function createSignal(value) {
  const read = () => value;
  const write = (newValue) => {
    value = newValue;
  };
  return [read, write];
}

 

Version 2 - CreateSignal & CreateEffect

Code to test:

import { createSignal, createEffect } from "./reactivy";

const [count, setCount] = createSignal(0);

createEffect(() => {
  console.log(count());
});

setCount(5);
  • stack: global state, everytime createEffectis called, will be added into stack
  • subscribers: for readfunction, get effect, add to subscribers, so that when value change, can notify signals
const stack = []

export function createSignal(value) {
  const subscribers = new Set();
  const read = () => {
    // check is there any effect runnning?
    // if yes, then add to subscribers
    const observer = stack[stack.length - 1];
    if (observer) {
      subscribers.add(observer);
    }
    return value;
  };
  const write = (newValue) => {
    value = newValue;
    // notify all the subscribers there is changes
    for (const observer of subscribers) {
      // add effect to the stack again
      observer.execute();
    }
  };
  return [read, write];
}

export function createEffect(fn) {
    const effect = {
       execute() {
           stack.push(effect);
           fn();
           stack.pop();
       }
    };
    
    effect.execute();
}

 

标签:function,const,Signal,value,stack,version,subscribers,createSignal,signal
From: https://www.cnblogs.com/Answer1215/p/17743485.html

相关文章

  • C error:deprecated conversion from string constant to 'char*' [-Wwrite-strings]
    问题描述解决C++中[Warning]deprecatedconversionfromstringconstantto'char*'[-Wwrite-strings]char*string="aaabbbcc";//warning的原因是字符串常量存放在const内存区...原因主程序初始化字符串,是字符串常量,该字符串的内存分配在全局的const内存区。......
  • Android获取VersionName
    使用privateStringversionName=BuildConfig.VERSION_NAME;//在需要的地方使用versionNamepublicStringgetVersionName(){returnversionName;}获取到的值一直是个固定值,https://cloud.tencent.com/developer/ask/sof/555589我们有一个releas......
  • Go每日一库之166:go-version(语义化版本)
    今天给大家推荐的是一个版本比较工具。该工具基于语义化标准的版本号进行比较、约束以及校验。以下是go-version的基本情况:安装通过goget进行安装:gogetgithub.com/hashicorp/go-version解析和比较版本号v1,err:=version.NewVersion("1.2")给版本号增加约束并校验v1......
  • Go - Using Multiple Versions of the Same Dependent Packages
    Problem: Youwanttousemultipleversionsofthesamedependentpackagesinyourcode.Solution: Usethereplacedirectiveinthego.modfiletorenameyourpackage.Thoughitmightseemlikeaverynicherequirement,thereissometimesaneedtobeabl......
  • Go - Requiring Local Versions of Dependent Packages
    Problem: Youwanttouselocalversionsofthedependentpackages.Solution: SetupGotouseavendordirectorybyrunninggomodvendor.Localversionsarethespecificversionofthedependentpackagesthatyoucanuseandareasafeguardincasethe......
  • Ubuntu系统自动更新导致| nvidia-smi命令报错Failed to initialize NVML: Driver/libr
    先查看日志cat/var/log/dpkg.log|grepnvidia发现早上ubuntu更新了nvidia驱动,两个nvidia驱动共存导致版本冲突了steponesudoapt-get--purgeremovenvidia*报错:steptwo根据报错的提示,输入:apt--fix-brokeninstall报错stepthree根据这篇大佬的博客输入......
  • Java序列serialVersionUID字段
    Spring框架默认使用Java的序列化机制,也就是说,Spring默认使用Java的内置序列化器。Java的序列化机制中,每个序列化的对象都有一个serialVersionUID字段,这个字段用来标识序列化对象的版本。Java的序列化机制是这样的:当一个对象被序列化时,Java会先检查对象的类是否有一个名为"serialV......
  • 配java环境在linux上,cuda9.2 old version
    主要参考:https://blog.csdn.net/m0_62946761/article/details/127138742先打个标记#CUDA10.2pipinstalltorch==1.6.0torchvision==0.7.0#CUDA10.1pipinstalltorch==1.6.0+cu101torchvision==0.7.0+cu101-fhttps://download.pytorch.org/whl/torch_stable.html#C......
  • docker-compose.yaml文件中的version定义和作用是什么
    在docker-compose.yaml文件中,version是DockerCompose文件的一个主要组成部分,用于指定当前DockerCompose文件的版本。DockerCompose是一个用于定义和运行多容器Docker应用程序的工具。它使用YAML文件来配置应用程序的服务,并使用一个文件来定义所有的相关设置。这个YAML文件被称......
  • Technocup 2022 - Elimination Round 3 B. Array Eversion
    给一个长度为\(n\)的数组。执行一次以下操作:让\(x=a_n\),然后数组\(a\)被分为左右两部分。左部分包含所有\(\leqx\)的元素,右部分包含所有\(>x\)的元素。且数组整体的原顺序不变。询问经过多少次操作后,数组不再改变?\(1\leqn\leq2\cdot10^5,1\leqa_i\le......