This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('publish/subscribe 3', () => { | |
it('should send a message from publish to subscribe', async () => { | |
let result; | |
const [promise, resolve] = triggerPromise(); | |
subscribe('ch1', (msg) => { | |
result = msg; | |
resolve(); | |
}); | |
publish('ch1', 1); | |
await asyncWithTimeout(promise); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const asyncWithTimeout = (promise, timeout = 900) => | |
Promise.race([ | |
promise, | |
new Promise((_, reject) => | |
setTimeout(() => reject(Error('Async timeout')), timeout) | |
), | |
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('publish/subscribe 2', () => { | |
it('should send a message from publish to subscribe', async () => { | |
let result; | |
const [promise, resolve] = triggerPromise(); | |
subscribe('ch1', (msg) => { | |
result = msg; | |
resolve(); | |
}); | |
publish('ch1', 1); | |
await promise; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('publish/subscribe 1', () => { | |
it('should send a message from publish to subscribe', async () => { | |
let result; | |
subscribe('ch1', (msg) => (result = msg)); | |
publish('ch1', 1); | |
await wait(900); | |
expect(result).toBe(1); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const func = (params = {}, data = {}) => { | |
const { | |
fallbackMessage: msg = 'Unknown', | |
offset = 4, | |
pos = {}, | |
...options | |
} = params; | |
const { x = 0, y = 0 } = pos; | |
const { title = '', items = [] } = data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const func = ( | |
{ | |
fallbackMessage: msg = 'Unknown', | |
offset = 4, | |
pos: { x = 0, y = 0 } = {}, | |
...options | |
} = {}, | |
data = {}, | |
) => { | |
const { title = '', items: [head, ...tail] = [] } = data; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Circle data object creator | |
const circle = (pos = [0, 0], r = 0) => ({ pos, r, hits: 0 }); | |
// Immutable-updates helpers | |
const incHits = obj => ({ ...obj, hits: (obj.hits || 0) + 1 }); | |
const setPos = (obj, pos = [0, 0]) => ({ ...obj, pos }); | |
// Circle computations | |
const area = r => r ** 2 * Math.PI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeCircle(pos = [0, 0], r = 0) { | |
let hits = 0; | |
const obj = { | |
pos, | |
getR: () => r, | |
getHits: () => hits, | |
incHits: () => (hits += 1), | |
area: () => r ** 2 * Math.PI, | |
intersect: (circle) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const deepFreeze = item => { | |
const complex = Object(item) === item; | |
if (Object.isFrozen(item) || !complex) return item; | |
Object.values(item).forEach(deepFreeze); | |
return Object.freeze(item); | |
}; | |
const createStore = (initState = {}, reducer) => { | |
let state = deepFreeze(initState); | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const makeCircle = (pos = [0, 0], r = 0) => { | |
let state = circle(pos, r); | |
return { | |
getState: () => state, | |
incHits: () => (state = incHits(state)), | |
setPos: pos => (state = setPos(state, pos)), | |
area: () => area(r), | |
intersect: c2 => { | |
let hit; | |
[hit, state] = intersect(state, c2.getState()); |
NewerOlder