Skip to content
Merged
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
59 changes: 44 additions & 15 deletions cpp/shared/core/EntryStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,56 @@ namespace react_native_smb {
// Append-only stream of listing entries owned by a task. It is also the single
// storage of the final listing result.
//
// Subscribers first receive everything appended so far as one batch, then each
// live batch. Listeners run under the lock so a replay can never be overtaken
// by a later batch; JS listeners only enqueue onto the JS thread, so this cannot
// re-enter.
// Delivery is notify-and-drain: appends wake each subscriber at most once until
// that subscriber drains. A busy JS thread therefore queues one pending signal
// per subscriber and receives every entry appended since its last drain as a
// single coalesced batch. JS drains once right after subscribe so late
// subscribers still get a replay of entries already stored. Signals run outside
// the lock and must not re-enter EntryStream synchronously.
class EntryStream {
public:
using Batch = std::vector<SmbFileInfo>;
using Listener = std::function<void(const Batch&)>;
using Signal = std::function<void()>;

void append(Batch batch) {
if (batch.empty()) return;
std::lock_guard<std::mutex> lock(mutex_);
for (auto& [id, listener] : listeners_) notify(listener, batch);
entries_.insert(entries_.end(), std::make_move_iterator(batch.begin()), std::make_move_iterator(batch.end()));
std::vector<Signal> pending;
{
std::lock_guard<std::mutex> lock(mutex_);
entries_.insert(entries_.end(), std::make_move_iterator(batch.begin()), std::make_move_iterator(batch.end()));
for (auto& [id, sub] : subscribers_) {
if (sub.scheduled) continue;
sub.scheduled = true;
if (sub.signal) pending.push_back(sub.signal);
}
}
for (const auto& signal : pending) notify(signal);
}

std::string subscribe(Listener listener) {
std::string subscribe(Signal signal) {
std::lock_guard<std::mutex> lock(mutex_);
if (!entries_.empty()) notify(listener, entries_);
std::string id = "ent_" + std::to_string(nextId_++);
listeners_.emplace(id, std::move(listener));
subscribers_.emplace(id, Subscriber{std::move(signal), 0, false});
return id;
}

void unsubscribe(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_);
listeners_.erase(id);
subscribers_.erase(id);
}

Batch drain(const std::string& id) {
std::lock_guard<std::mutex> lock(mutex_);
auto it = subscribers_.find(id);
if (it == subscribers_.end()) return {};
Subscriber& sub = it->second;
Batch out;
if (sub.cursor < entries_.size()) {
out.assign(entries_.begin() + static_cast<std::ptrdiff_t>(sub.cursor), entries_.end());
sub.cursor = entries_.size();
}
sub.scheduled = false;
return out;
}

template <typename Fn>
Expand All @@ -50,16 +73,22 @@ class EntryStream {
}

private:
static void notify(const Listener& listener, const Batch& batch) {
struct Subscriber {
Signal signal;
size_t cursor{0};
bool scheduled{false};
};

static void notify(const Signal& signal) {
try {
if (listener) listener(batch);
if (signal) signal();
} catch (...) {
}
}

mutable std::mutex mutex_;
Batch entries_;
std::map<std::string, Listener> listeners_;
std::map<std::string, Subscriber> subscribers_;
size_t nextId_{1};
};

Expand Down
3 changes: 3 additions & 0 deletions cpp/shared/core/SmbTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ std::string SmbTask::subscribeEntries(const EntriesListener& listener) {

void SmbTask::unsubscribeEntries(const std::string& id) { entries_->unsubscribe(id); }

std::vector<SmbFileInfo> SmbTask::drainEntries(const std::string& id) { return entries_->drain(id); }

void SmbTask::loadHybridMethods() {
HybridObject::loadHybridMethods();
registerHybrids(this, [](Prototype& prototype) {
Expand All @@ -148,6 +150,7 @@ void SmbTask::loadHybridMethods() {
prototype.registerHybridMethod("unsubscribe", &SmbTask::unsubscribe);
prototype.registerHybridMethod("subscribeEntries", &SmbTask::subscribeEntries);
prototype.registerHybridMethod("unsubscribeEntries", &SmbTask::unsubscribeEntries);
prototype.registerHybridMethod("drainEntries", &SmbTask::drainEntries);
prototype.registerRawHybridMethod("getResultValue", 0, &SmbTask::getResultValueRaw);
});
}
Expand Down
5 changes: 3 additions & 2 deletions cpp/shared/core/SmbTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ using namespace margelo::nitro;

class SmbTask : public HybridObject, public SmbTaskCore {
public:
using EntriesListener = std::function<void(const std::vector<SmbFileInfo>&)>;
using EntriesListener = std::function<void()>;

SmbTask(std::string id, std::unique_ptr<OperatorBase> seedOp, std::shared_ptr<SmbConnectionPool> pool);
~SmbTask();
Expand All @@ -41,9 +41,10 @@ class SmbTask : public HybridObject, public SmbTaskCore {
std::string subscribeMap(const std::function<void(const std::unordered_map<std::string, std::string>&)>& listener);
void unsubscribe(const std::string& id) { SmbTaskCore::unsubscribe(id); }

// Incremental listing entries (replays entries emitted before subscribing).
// Incremental listing entries via notify-and-drain (coalesced while JS is busy).
std::string subscribeEntries(const EntriesListener& listener);
void unsubscribeEntries(const std::string& id);
std::vector<SmbFileInfo> drainEntries(const std::string& id);
const std::shared_ptr<EntryStream>& entryStream() const { return entries_; }

std::future<void> result() { return completionFuture(); }
Expand Down
5 changes: 3 additions & 2 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ try {
```

- Every streamed entry is final: directories already carry `childCount`, and `securityDescriptor` is set when requested.
- A batch is pushed once `batchSize` entries are ready or 100 ms after the previous batch; the first batch goes out as soon as anything is ready. A batch contains every entry ready at that moment, so it can be larger than `batchSize`.
- Native flushes when `batchSize` entries are ready or 100 ms after the previous flush (the first flush goes out as soon as anything is ready). Each flush takes every entry ready at that moment, so it can be larger than `batchSize`.
- Delivery to JS is notify-and-drain: a busy JS thread coalesces several native flushes into one `subscribeEntries` callback.
- Subscribing late loses nothing: the first callback replays every entry emitted so far.
- Entries arrive in server order. Sort in the UI if you need a stable order.
- If the task fails or is cancelled, entries already received are an incomplete listing.
Expand Down Expand Up @@ -126,7 +127,7 @@ See [Connection pool](./ARCHITECTURE.md).

| Member | Description |
| ------ | ----------- |
| `subscribeEntries(listener)` | Receive entry batches (replays earlier ones first); returns unsubscribe |
| `subscribeEntries(listener)` | Receive entry batches (replays earlier ones first; coalesces while JS is busy); returns unsubscribe |

### Errors

Expand Down
2 changes: 1 addition & 1 deletion docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ A recursive copy sends its first non-empty file alone to find out whether the se
└ per entry (opt-in): CREATE + QUERY_INFO + CLOSE security descriptor
```

Lookups share one connection: up to 16 directory queries (expansions and counts) and, separately, up to 32 security-descriptor queries in flight. Lookups start in top-level order — everything under an earlier top-level entry before anything under a later one — and directory queries go on the wire before security descriptors. An entry is ready once its lookups finish — for an expanded directory, also all of its children, whose count becomes `childCount` without another query, so each directory is opened once. Ready top-level entries are emitted in server order through the task's `EntryStream` (`subscribeEntries`), which also stores the final result.
Lookups share one connection: up to 16 directory queries (expansions and counts) and, separately, up to 32 security-descriptor queries in flight. Lookups start in top-level order — everything under an earlier top-level entry before anything under a later one — and directory queries go on the wire before security descriptors. An entry is ready once its lookups finish — for an expanded directory, also all of its children, whose count becomes `childCount` without another query, so each directory is opened once. Ready top-level entries are appended in server order to the task's `EntryStream`, which also stores the final result. Each subscriber is woken with a payload-free signal at most once until it drains; `subscribeEntries` then pulls every entry since the last drain as one coalesced batch, so a busy JS thread does not queue one callback per native flush.

The same engine backs every other directory walk through `listing::listOnCtx`, a non-streaming call on an already-held slot; libsmb2's `opendir` is no longer used:

Expand Down
4 changes: 2 additions & 2 deletions docs/TASK_MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function DownloadBar({ task }) {

## Stream listing entries

`listDirectory` returns an `SmbListTask`. Besides status snapshots, a listing pushes batches of top-level entries through a separate channel — no `progress` is involved:
`listDirectory` returns an `SmbListTask`. Besides status snapshots, a listing delivers top-level entries through a separate channel — no `progress` is involved:

```typescript
const task = smb.listDirectory('/Media');
Expand All @@ -103,7 +103,7 @@ await task.wait(); // completion without converting the full result again
unsub();
```

`subscribeEntries` replays the entries emitted before the call, so it can be attached at any time. All batches are delivered before the task's terminal status. `result()` still resolves with the complete array, so existing callers are unaffected.
`subscribeEntries` replays the entries emitted before the call, so it can be attached at any time. Native flushes wake a payload-free signal; the JS wrapper drains every entry appended since the last wake, so a busy JS thread coalesces several flushes into one batch. All entry deliveries run before the task's terminal status. `result()` still resolves with the complete array, so existing callers are unaffected.

## Fire-and-forget

Expand Down
7 changes: 4 additions & 3 deletions src/core/SmbClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ export class SmbClient {

/**
* List a directory. Top-level entries also stream via
* {@link SmbListTask.subscribeEntries}: a batch is pushed once `batchSize`
* entries are ready or 100 ms have passed (the first batch goes out immediately).
* With a non-zero `maxDepth`, each streamed entry carries its complete subtree.
* {@link SmbListTask.subscribeEntries}. `batchSize` and the 100 ms delay control
* native flushes (the first flush goes out immediately); JS may receive several
* flushes as one coalesced batch while the thread is busy. With a non-zero
* `maxDepth`, each streamed entry carries its complete subtree.
*/
listDirectory(path: string, options: ListDirectoryOptions = {}): SmbListTask {
const { maxDepth = 0, includeSecurityDescriptor = false, batchSize = DEFAULT_LIST_BATCH_SIZE } = options;
Expand Down
23 changes: 17 additions & 6 deletions src/core/SmbListTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { SmbTask } from './SmbTask';
* Task returned by `listDirectory`. Top-level entries are delivered in batches
* while the listing is still running; `result()` still resolves with the
* complete list.
*
* Native flushes wake a payload-free signal; this wrapper drains coalesced
* entries so a busy JS thread receives fewer, larger batches.
*/
export class SmbListTask extends SmbTask<SmbFileInfo[]> {
constructor(
Expand All @@ -17,8 +20,9 @@ export class SmbListTask extends SmbTask<SmbFileInfo[]> {
/**
* Receive top-level entries as they become available, in server order. The
* first call delivers every entry emitted so far, so subscribing late loses
* nothing. Each entry is final: directories carry `childCount`, and with a
* non-zero `maxDepth` their complete `children` subtree.
* nothing. While the JS thread is busy, native flushes coalesce into one
* batch per wake. Each entry is final: directories carry `childCount`, and
* with a non-zero `maxDepth` their complete `children` subtree.
*
* Entries received before a failure or cancellation are an incomplete listing.
*
Expand All @@ -30,15 +34,22 @@ export class SmbListTask extends SmbTask<SmbFileInfo[]> {
*/
subscribeEntries(listener: (batch: SmbFileInfo[]) => void): () => void {
let active = true;
const subscriptionId = this.nativeList.subscribeEntries((batch) => {
if (active) listener(batch);
});
let subscriptionId: string | null = null;

const deliver = () => {
if (!active || subscriptionId == null) return;
const batch = this.nativeList.drainEntries(subscriptionId);
if (batch.length > 0) listener(batch);
};

subscriptionId = this.nativeList.subscribeEntries(deliver);
deliver();

return () => {
if (!active) return;
active = false;
try {
this.nativeList.unsubscribeEntries(subscriptionId);
this.nativeList.unsubscribeEntries(subscriptionId!);
} catch {}
};
}
Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ export interface NativeSmbTask {
}

export interface NativeSmbListTask extends NativeSmbTask {
/** Replays entries emitted so far as one batch, then delivers each live batch. */
subscribeEntries(listener: (entries: SmbFileInfo[]) => void): string;
/**
* Payload-free wake for new entries. Call {@link drainEntries} to pull every
* entry appended since the last drain (including a late-subscribe replay).
*/
subscribeEntries(listener: () => void): string;
unsubscribeEntries(subscriptionId: string): void;
drainEntries(subscriptionId: string): SmbFileInfo[];
}
Loading
Loading