Skip to content

Commit

Permalink
feat: remove singleton abstraction again
Browse files Browse the repository at this point in the history
  • Loading branch information
betula committed Jun 17, 2022
1 parent 5692e8c commit 79b1a95
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 102 deletions.
3 changes: 0 additions & 3 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"testEnvironment": "jest-environment-jsdom",
"setupFilesAfterEnv": [
"<rootDir>/jest"
],
"modulePathIgnorePatterns": [
"<rootDir>/src/"
],
Expand Down
1 change: 0 additions & 1 deletion jest/index.js

This file was deleted.

12 changes: 0 additions & 12 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export {
event, fire, filter, map,
unsubs, un,
batch, untrack,
single, free, mock, unmock, clear,
observe, useBox, useJsx,
useBoxes,
useLogic, useWrite,
Expand Down Expand Up @@ -47,17 +46,6 @@ declare function un(...args: any[]): any;
declare function batch(...args: any[]): any;
declare function untrack(...args: any[]): any;


declare const single: {
<M>(target: (new () => M) | (() => M)): M;
}

declare function free(...args: any[]): any;
declare function mock(...args: any[]): any;
declare function unmock(...args: any[]): any;
declare function clear(...args: any[]): any;


declare const observe: any;

declare const useBox: <P>(box: Box<P>) => P;
Expand Down
45 changes: 2 additions & 43 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ try {

const key_remini = '.remini';
const key_fn = 'fn';
const key_all = 'all';
const key_unsafe = 'unsafe';
const key_untrack = 'untrack';
const key_function = 'function';
Expand Down Expand Up @@ -193,11 +192,9 @@ const map = (r, fn) => (


//
// Single
// Instantiate
//

const single_map = new Map();

const _inst = (target, args) => {
args = args || [];
let instance, unsub;
Expand All @@ -215,44 +212,6 @@ const _inst = (target, args) => {
return [instance, unsub];
};

const single = (target) => {
let rec = single_map.get(target);
if (!rec) {
rec = _inst(target);
single_map.set(target, rec);
}
return rec[0];
};

const free = (...targets) => {
try {
targets.forEach((target) => {
const rec = single_map.get(target);
rec && rec[1]();
});
} finally {
targets.forEach((target) => single_map.delete(target));
}
};
free[key_all] = () => {
single_map.forEach((h) => h[1]());
single_map.clear();
};

const mock = (target, mocked) => (
single_map.set(target, [mocked, () => {}, 1]),
mocked
);

const unmock = (...targets) => (
targets.forEach(target => single_map.delete(target))
);
unmock[key_all] = () => (
single_map.forEach((h, k) => h[2] && single_map.delete(k))
);

const clear = () => single_map.clear();


//
// React bindings
Expand Down Expand Up @@ -353,6 +312,7 @@ const useLogic = (target, deps) => {

const useWrite = write;


//
// Exports
//
Expand All @@ -363,7 +323,6 @@ module.exports = {
event, fire, filter, map,
unsubs, un,
batch, untrack,
single, free, mock, unmock, clear,
observe, useBox, useJsx,
useBoxes,
useLogic, useWrite,
Expand Down
43 changes: 0 additions & 43 deletions tests/single.test.ts

This file was deleted.

0 comments on commit 79b1a95

Please sign in to comment.