Skip to content
This repository has been archived by the owner on Dec 31, 2022. It is now read-only.

Feature: useDeferred(...) #18

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
node_modules/
dist/
coverage/
.vscode/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I normally have formatOnSave set to true, so for this repo specifically I set it to false. Didn't want to pollute it with my editor settings so I figured this would be the best course of action.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd instead add stuff to your .git/info/exclude or a user-global alternative.

22 changes: 20 additions & 2 deletions src/tng-hooks.src.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

return {
TNG, useState, useReducer, useEffect,
useMemo, useCallback, useRef,
useMemo, useCallback, useRef, useDeferred
};


Expand Down Expand Up @@ -295,5 +295,23 @@
else {
throw new Error("useRef() only valid inside an Articulated Function or a Custom Hook.");
}
}
}

function useDeferred() {
if (getCurrentBucket()) {
const [deferred] = useState((function () {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Critical Area: Would love some insight on other ways to generate a Deferred object as an initial value. an IIFE that creates one was the best that I could come up with.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you can do this without support form upstream, what's the value of this being included here? You're setting yourself up for a leaking batteries problem, no?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the value in having this function implemented in this package. Can you help me understand why you see value in that?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Derp, never mind me. Just realized that this was invited by getify on #17. I don't think it's wise to lump stuff together before there's good evidence for it being the default, but I also don't think it's wise to go against the maintainer's view.

let resolve, reject;
let pr = new Promise((res, rej) => {
resolve = res;
reject = rej;
})

return { pr, resolve, reject }
})())

return deferred
} else {
throw new Error("useDeferred() only valid inside an Articulated Function or a Custom Hook.")
}
}
});
63 changes: 63 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,69 @@ QUnit.test( "useRef(..)", function test(assert){
assert.strictEqual( pActual, pExpected, "call without TNG wrapping context" );
} );

QUnit.test("useDeferred(..)", function test(assert) {
var done = assert.async(2);

var fooExpected = [
"foo - setup",
"foo - before resolve",
"foo - after resolve",
"bar - reject",
"foo - resolve"
]

function foo() {
var d = useDeferred();

assert.step("foo - setup");
d.pr.then(payload => {
assert.step(`${payload}`)
assert.verifySteps(fooExpected, "check order");
done();
})

assert.step("foo - before resolve");
d.resolve("foo - resolve")
assert.step("foo - after resolve")
}

function bar() {
var d = useDeferred();

d.pr.then(payload => {
assert.step("shouldn't hit")
}, err => {
assert.step(err)
done();
})

d.reject("bar - reject")
}

function baz() {
var d = useDeferred();

return "nope 1";
}

var bazExpected = "error";
var bazActual;

foo = TNG(foo);
bar = TNG(bar);
bar();
foo()

try {
baz();
} catch (err) {
bazActual = "error";
}

assert.expect(7) // note: 2 assertions + 5 `step(...)` calls
assert.strictEqual(bazActual, bazExpected, "call without TNG wrapping context")
});

QUnit.test( "use hooks from custom hook", function test(assert){
function foo() {
var [x,setX] = useState( -1 );
Expand Down