This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
150 lines (126 loc) · 3.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as React from "react";
export interface StoreProps<T> {
children?: (value: T, previousValue: T | undefined) => React.ReactNode;
}
type NonFunction =
| object
| string
| boolean
| number
| symbol
| null
| undefined;
function createContainer<VALUE, MSG extends NonFunction>(
containerName: string,
initialValue: VALUE,
onMessage: (value: VALUE, msg: MSG) => void
) {
let previous: { value: VALUE } | null = null;
let current: { value: VALUE } = { value: initialValue };
const instances: Array<React.Component<StoreProps<VALUE>>> = [];
class Container extends React.Component<StoreProps<VALUE>> {
static get displayName() {
return containerName;
}
static get value() {
return current.value;
}
static set value(nextValue: VALUE) {
previous = current;
current = { value: nextValue };
for (const instance of instances) {
if (!instance) {
throw new Error("Whisper expected non-null enlisted");
}
if (!instance.forceUpdate) {
throw new Error("Whisper expected component enlisted");
}
instance.forceUpdate();
}
}
constructor(props: StoreProps<VALUE>) {
super(props);
instances.push(this);
}
componentWillUnmount() {
instances.splice(instances.indexOf(this), 1);
}
static next(action: MSG) {
onMessage(current.value, action);
}
static update(action: ((prev: VALUE) => MSG)) {
onMessage(current.value, action(current.value));
}
render() {
const { children } = this.props;
if (!children) {
return null;
}
if (typeof children !== "function") {
throw new Error("Container child should be a function");
}
return children(current.value, previous ? previous.value : undefined);
}
}
return Container;
}
function createStore<VALUE extends NonFunction>(initialValue: VALUE) {
const Container = createContainer<VALUE, VALUE>(
"Store",
initialValue,
handleMessage
);
function handleMessage(value: VALUE, msg: VALUE) {
Container.value = msg;
}
return Container;
}
function createReducer<VALUE, MSG extends NonFunction>(
initialValue: VALUE,
reductor: (state: VALUE, action: MSG) => VALUE
) {
const Container = createContainer<VALUE, MSG>(
"Reducer",
initialValue,
handleMessage
);
function handleMessage(value: VALUE, msg: MSG) {
Container.value = reductor(value, msg);
}
return Container;
}
function createActor<VALUE, MSG extends NonFunction>(
initialValue: VALUE,
actor: (msgBox: AsyncIterable<MSG>, next: (value: VALUE) => void) => any
) {
const queue: MSG[] = [];
let releaseDispencer: Function | null = null;
const Container = createContainer<VALUE, MSG>(
"Actor",
initialValue,
handleMessage
);
function handleMessage(value: VALUE, msg: MSG) {
queue.push(msg);
if (releaseDispencer) {
releaseDispencer();
}
}
actor(
(async function*() {
while (true) {
await new Promise(r => {
releaseDispencer = r;
});
while (queue.length) {
yield queue.splice(0, 1)[0];
}
}
})(),
v => {
Container.value = v;
}
);
return Container;
}
export { createStore, createReducer, createActor };